#if ( !defined __PROXY_READER_HPP__ )
#define __PROXY_READER_HPP__

#define SQL_PROXIES_CREATE_TABLE	"CREATE TABLE t_proxies( \
										id INTEGER PRIMARY KEY, \
										proxy VARCHAR(200) UNIQUE NOT NULL, \
										status INTEGER DEFAULT(0), \
										response_time INTEGER DEFAULT(9999), \
										create_time DATE, \
										update_time DATE )"

#define SQL_PROXIES_INSERT			"INSERT INTO t_proxies( \
										proxy, \
										status, \
										create_time, \
										update_time) \
									VALUES( \
										?, \
										?, \
										DATETIME('NOW'), \
										DATETIME('NOW'))"

#define SQL_PROXIES_UPDATE			"UPDATE t_proxies SET \
										status=?, \
										response_time=?"

#define SQL_PROXIES_SELECT			"SELECT \
										proxy, \
										status, \
										response_time, \
										create_time, \
										update_time \
									FROM \
										t_proxies"

#define SQL_PROXIES_SELECT_COUNT	"SELECT \
										count(1) \
									FROM \
										t_proxies"

#include <string>
#include <vector>

#include "sqlite3/sqlite3_wrapper.hpp"

using namespace std;
using namespace sqlite3_wrapper;

class ProxyReader
{
public:
	ProxyReader ( const string& db_file );
	~ProxyReader ();

public:
	void Insert ( const string& proxy, int status );
	void Insert ( const vector < string >& proxies, int status );

	void Update ( const string& proxy, int status, int response_time );
	void Update ( const vector < string >& proxies, int status, int response_time );
	void UpdateAll ( int status, int response_time );
	
	void Delete ( const string& proxy );
	void Delete ( const vector < string >& proxies );
	void Delete ( int status );
	void DeleteAll ();

	void SelectWithStatus ( int status, vector < string >& proxies );
	void SelectWithResponseTime ( int response_time, vector < string >& proxies );

	void SplitProxy ( const string& proxy, string& host, string& port );
	void MergeProxy ( const string& host, const string& port, string& proxy );
protected:
	void open_database ( const string& db_file );
	void close_database ();
	void read_select_results ( sqlite3_reader& reader, vector < string >& proxies );
private:
	sqlite3_connection* _connection;
};

#endif // __PROXY_READER_HPP__